package com.manulife.hk.performance.converter;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;


import microsoft.sql.DateTimeOffset;

public class DatetimeOffsetType implements UserType {


	private static Method methodSetDateTimeOffset;

	@Override
	public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
		return arg0;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		DateTimeOffset dateTimeOffset = (DateTimeOffset) value;
		return (value == null) ? null
				: dateTimeOffset;
	}

	@Override
	public Serializable disassemble(Object arg0) throws HibernateException {
		return (Serializable) arg0;
	}

	@Override
	public boolean equals(Object arg0, Object arg1) throws HibernateException {
		if (arg0 == null || !(arg0 instanceof DateTimeOffset) || arg1 == null || !(arg1 instanceof DateTimeOffset)) {
			return false;
		}

		return arg0.equals(arg1);
	}

	@Override
	public int hashCode(Object arg0) throws HibernateException {
		return arg0.hashCode();
	}

	@Override
	public boolean isMutable() {
		return true;
	}

	@Override
	public Object replace(Object original, Object target, Object arg2) throws HibernateException {
		// TODO Auto-generated method stub
		System.out.println("replace");
		return null;
	}

	@Override
	public Class<DateTimeOffset> returnedClass() {
		return DateTimeOffset.class;
	}

	@Override
	public int[] sqlTypes() {
		return new int[] { microsoft.sql.Types.DATETIMEOFFSET }; // -155
	}


	@Override
	public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
			throws HibernateException, SQLException {
		return (DateTimeOffset) resultSet.getObject(names[0]);

	}

	@Override
	public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session)
			throws HibernateException, SQLException {
		preparedStatement.setString(index, value.toString());
	}
}
